#include "ScnsColor.h"

#if defined(SCNS_COLOR_ENABLE)&&SCNS_COLOR_ENABLE==1

const ScnsColorArgb8888 scnsColorRandomMap[256]={
    0XFFAB93C7,0XFFEBA39D,0XFF92F00A,0XFF126FA3,0XFFB9E691,0XFF8F7536,0XFFD825F6,0XFF4995CF,
    0XFFAC2A73,0XFFACAE38,0XFF0EBF04,0XFF9E806B,0XFF36A326,0XFFE4129B,0XFF3F9EEB,0XFF3660DC,
    0XFF97EE77,0XFF253287,0XFF77507A,0XFFDD89F1,0XFF21234C,0XFFD072C4,0XFFAFC9A7,0XFF509F87,
    0XFFF83B63,0XFF6D9013,0XFF3F3F4A,0XFFB3DF24,0XFF441EFF,0XFFAC750F,0XFFC17A93,0XFFE7ACD0,
    0XFF8964E6,0XFF2DDC54,0XFF66325E,0XFFEF56AE,0XFF3223BE,0XFF53382F,0XFFE99E4C,0XFF2750C2,
    0XFF78CA37,0XFFEC37CB,0XFFC42D40,0XFF8B029E,0XFF8935AD,0XFF37AA69,0XFF7DB4D0,0XFF0EDC14,
    0XFF8063AA,0XFF488A55,0XFFF97EA9,0XFFA7A915,0XFF5B6233,0XFFA80CBE,0XFFF1F1C8,0XFF7B906F,
    0XFF306F89,0XFF7779C4,0XFFC0B0DF,0XFF30E02B,0XFF52688A,0XFF286C0F,0XFFB26229,0XFFD03894,
    0XFFCEBA7D,0XFF93E8E7,0XFFE364F7,0XFFEE0714,0XFF7AA84C,0XFFA53829,0XFFDBF5FC,0XFFC43EE5,
    0XFF79163B,0XFF6DF435,0XFFC6C137,0XFF18DF7E,0XFF261A27,0XFF57AE55,0XFF22B8AC,0XFF5BE1A3,
    0XFF9BE458,0XFF56DC35,0XFF2F8BFE,0XFFC722C0,0XFF29BA8B,0XFF440F8E,0XFF08966B,0XFF95D11C,
    0XFFD47047,0XFF2EFDB8,0XFFF4105F,0XFF3200B0,0XFF68189F,0XFFDF4824,0XFFB65480,0XFF3D7B32,
    0XFF5CC46F,0XFFD86298,0XFF5741F4,0XFFAD4BD6,0XFF0187EE,0XFF702886,0XFF58FE6F,0XFFD81C0A,
    0XFFF1D441,0XFF330765,0XFF014450,0XFF6BDDCB,0XFF068DBD,0XFF7167FA,0XFF8AEE3B,0XFF0E7639,
    0XFF6B7214,0XFF6E8952,0XFFC3E3D9,0XFF155B59,0XFFAFB3BF,0XFF6F35CA,0XFF6FE408,0XFF40C1E6,
    0XFF379BAB,0XFFFFCC80,0XFFACC9F9,0XFF6B0064,0XFF3E1903,0XFFF05F3B,0XFF707DE4,0XFFB60F4F,
    0XFF4243B5,0XFFB2977C,0XFF7F0CC6,0XFF54A5B1,0XFFADCBD4,0XFF65A1F2,0XFF9C27C9,0XFF7FBDF6,
    0XFF41EF03,0XFFA75A9C,0XFF8B210C,0XFF28DBDE,0XFF35F571,0XFFE1CA1C,0XFFE1F666,0XFF48674A,
    0XFF3A4509,0XFFE2F91C,0XFF44E0F4,0XFF938FFB,0XFF996BCA,0XFF739A8B,0XFF5A3597,0XFFF65B06,
    0XFFDDF596,0XFF46F62C,0XFF0159D0,0XFF56C60F,0XFF3EFC4F,0XFF9E939C,0XFF928A0F,0XFF8EB0A3,
    0XFF0AF1AB,0XFFE72A7B,0XFF783F49,0XFF0112E2,0XFF051268,0XFFF7C9FC,0XFFBB9C54,0XFF20EA00,
    0XFF123526,0XFFA2B15A,0XFF5C3703,0XFFF58272,0XFFFF01FA,0XFF7AFA75,0XFF09F358,0XFF84CE8B,
    0XFFADFD15,0XFF12CC4C,0XFF3240E8,0XFFC54707,0XFF2381B6,0XFFB7FF3A,0XFF1B396A,0XFF60829B,
    0XFF04B4EC,0XFF3C093F,0XFF62BA96,0XFF950E57,0XFF064FFA,0XFFA403FC,0XFF023E9E,0XFF07D1D7,
    0XFFEC0FD5,0XFFFA83CF,0XFF1429BE,0XFF8147FF,0XFF11521C,0XFF7DE5A1,0XFF4C33D8,0XFF18ACD3,
    0XFFFA1FBA,0XFFD6D1AE,0XFFD21337,0XFF9E5262,0XFF5DC2D5,0XFF0FF9E5,0XFF21FC2C,0XFF129C49,
    0XFFE8565C,0XFF16DAFA,0XFF444576,0XFF62F6DB,0XFFDA9732,0XFFD9D4F8,0XFF17912D,0XFFD3B6BB,
    0XFFE856D8,0XFF468F11,0XFFC4F403,0XFFCD1864,0XFFCC97A4,0XFFE38D07,0XFF03B83A,0XFFD97374,
    0XFFB90302,0XFF83A708,0XFFA1FAB2,0XFF8E2973,0XFFF8C264,0XFF840000,0XFFB84C3D,0XFF762C20,
    0XFFC8BE0B,0XFFDF3949,0XFF6C7168,0XFF6D18F0,0XFF3E64FD,0XFF675BCD,0XFF30676B,0XFF7B4E16,
    0XFF2A9804,0XFF0B08C1,0XFF6DEAFD,0XFF3180DD,0XFFA22299,0XFF26BAFD,0XFFCE6D15,0XFFA60879,
    0XFF5A0E24,0XFF19F790,0XFFAEA5FA,0XFF900425,0XFF1D72F2,0XFFF6E21C,0XFFFBFDFE,0XFF000000,
};

void scnsColorGet(const void*p,ScnsColorFormat format,const ScnsColorArgb8888*cult,uint8*a,uint8*r,uint8*g,uint8*b)
{
    switch(format)
    {
        case SCNS_COLOR_FORMAT_GRAY8:
        {
            if(a)*a=scnsColorGray8ToA(*((const ScnsColorGray8*)p));
            if(r)*r=scnsColorGray8ToR(*((const ScnsColorGray8*)p));
            if(g)*g=scnsColorGray8ToG(*((const ScnsColorGray8*)p));
            if(b)*b=scnsColorGray8ToB(*((const ScnsColorGray8*)p));
            break;
        }
        case SCNS_COLOR_FORMAT_CLUT8:
        {
            if(cult)
            {
                if(a)*a=scnsColorArgb8888ToA(cult[*((const ScnsColorClut8*)p)]);
                if(r)*r=scnsColorArgb8888ToR(cult[*((const ScnsColorClut8*)p)]);
                if(g)*g=scnsColorArgb8888ToG(cult[*((const ScnsColorClut8*)p)]);
                if(b)*b=scnsColorArgb8888ToB(cult[*((const ScnsColorClut8*)p)]);
            }
            //else
            //{
            //    if(a)*a=scnsColorClut8ToA(*((const ScnsColorClut8*)p));
            //    if(r)*r=scnsColorClut8ToR(*((const ScnsColorClut8*)p));
            //    if(g)*g=scnsColorClut8ToG(*((const ScnsColorClut8*)p));
            //    if(b)*b=scnsColorClut8ToB(*((const ScnsColorClut8*)p));
            //}
            break;
        }
        case SCNS_COLOR_FORMAT_BINARY8:
        {
            if(a)*a=scnsColorBinary8ToA(*((const ScnsColorBinary8*)p));
            if(r)*r=scnsColorBinary8ToR(*((const ScnsColorBinary8*)p));
            if(g)*g=scnsColorBinary8ToG(*((const ScnsColorBinary8*)p));
            if(b)*b=scnsColorBinary8ToB(*((const ScnsColorBinary8*)p));
            break;
        }
        case SCNS_COLOR_FORMAT_RGB565:
        {
            if(a)*a=scnsColorRgb565ToA(*((const ScnsColorRgb565*)p));
            if(r)*r=scnsColorRgb565ToR(*((const ScnsColorRgb565*)p));
            if(g)*g=scnsColorRgb565ToG(*((const ScnsColorRgb565*)p));
            if(b)*b=scnsColorRgb565ToB(*((const ScnsColorRgb565*)p));
            break;
        }
        case SCNS_COLOR_FORMAT_RGB888:
        {
            if(a)*a=scnsColorRgb888ToA(*((const ScnsColorRgb888*)p));
            if(r)*r=scnsColorRgb888ToR(*((const ScnsColorRgb888*)p));
            if(g)*g=scnsColorRgb888ToG(*((const ScnsColorRgb888*)p));
            if(b)*b=scnsColorRgb888ToB(*((const ScnsColorRgb888*)p));
            break;
        }
        case SCNS_COLOR_FORMAT_ARGB8888:
        {
            if(a)*a=scnsColorArgb8888ToA(*((const ScnsColorArgb8888*)p));
            if(r)*r=scnsColorArgb8888ToR(*((const ScnsColorArgb8888*)p));
            if(g)*g=scnsColorArgb8888ToG(*((const ScnsColorArgb8888*)p));
            if(b)*b=scnsColorArgb8888ToB(*((const ScnsColorArgb8888*)p));
            break;
        }
        case SCNS_COLOR_FORMAT_ARGB4444:
        {
            if(a)*a=scnsColorArgb4444ToA(*((const ScnsColorArgb4444*)p));
            if(r)*r=scnsColorArgb4444ToR(*((const ScnsColorArgb4444*)p));
            if(g)*g=scnsColorArgb4444ToG(*((const ScnsColorArgb4444*)p));
            if(b)*b=scnsColorArgb4444ToB(*((const ScnsColorArgb4444*)p));
            break;
        }
        default:
        {
            if(a)*a=0;
            if(r)*r=0;
            if(g)*g=0;
            if(b)*b=0;
        }
    }
}

void scnsColorSet(void*p,ScnsColorFormat format,uint8 a,uint8 r,uint8 g,uint8 b)
{
    switch(format)
    {
        case SCNS_COLOR_FORMAT_GRAY8:
        {
            *((ScnsColorGray8*)p)=scnsColorARGBToGray8(a,r,g,b);
            break;
        }
        //case SCNS_COLOR_FORMAT_CLUT8:
        //{
        //    *((ScnsColorClut8*)p)=scnsColorARGBToClut8(a,r,g,b);
        //    break;
        //}
        case SCNS_COLOR_FORMAT_BINARY8:
        {
            *((ScnsColorBinary8*)p)=scnsColorARGBToBinary8(a,r,g,b);
            break;
        }
        case SCNS_COLOR_FORMAT_RGB565:
        {
            *((ScnsColorRgb565*)p)=scnsColorARGBToRgb565(a,r,g,b);
            break;
        }
        case SCNS_COLOR_FORMAT_RGB888:
        {
            *((ScnsColorRgb888*)p)=scnsColorARGBToRgb888(a,r,g,b);
            break;
        }
        case SCNS_COLOR_FORMAT_ARGB8888:
        {
            *((ScnsColorArgb8888*)p)=scnsColorARGBToArgb8888(a,r,g,b);
            break;
        }
        case SCNS_COLOR_FORMAT_ARGB4444:
        {
            *((ScnsColorArgb4444*)p)=scnsColorARGBToArgb4444(a,r,g,b);
            break;
        }
        default:;
    }
}

inline void scnsColorMerge(uint8 fa,uint8 fr,uint8 fg,uint8 fb,uint8 ba,uint8 br,uint8 bg,uint8 bb,uint8*a,uint8*r,uint8*g,uint8*b)
{
    int32 ma=(((int32)fa)*((int32)ba))/255;
    int32 oa=fa+ba-ma;
    if(a)*a=oa;
    if(oa)
    {
        if(r)*r=(uint8)((((int32)fr)*((int32)fa)+((int32)br)*((int32)ba)-((int32)br)*((int32)ma))/oa);
        if(g)*g=(uint8)((((int32)fg)*((int32)fa)+((int32)bg)*((int32)ba)-((int32)bg)*((int32)ma))/oa);
        if(b)*b=(uint8)((((int32)fb)*((int32)fa)+((int32)bb)*((int32)ba)-((int32)bb)*((int32)ma))/oa);
    }
}

#endif